package com.example.rtsp2.controller;

import com.example.rtsp2.handler.VideoStreamHandler;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 杜科璇
 * @Description: 流控制器
 * @Date: 2025/9/5 19:07
 * @Version: 1.0
 */

@RestController
@RequestMapping("/api/streams")
public class StreamController {

    private final VideoStreamHandler videoStreamHandler;

    public StreamController(VideoStreamHandler videoStreamHandler) {
        this.videoStreamHandler = videoStreamHandler;
    }

    // 获取所有流状态
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getStatus() {
        Map<String, Object> status = new HashMap<>();
        status.put("activeConnections", videoStreamHandler.getActiveSessionCount());
        status.put("streams", videoStreamHandler.getStreamStatus());
        status.put("status", "RUNNING");
        return ResponseEntity.ok(status);
    }

    // 添加新的RTSP流
    @PostMapping("/add")
    public ResponseEntity<String> addStream(@RequestBody Map<String, String> request) {
        String streamId = request.get("streamId");
        String rtspUrl = request.get("rtspUrl");

        if (streamId == null || rtspUrl == null) {
            return ResponseEntity.badRequest().body("streamId和rtspUrl被拒绝");
        }

        boolean success = videoStreamHandler.addStream(streamId, rtspUrl);
        if (success) {
            return ResponseEntity.ok("流创建成功...");
        } else {
            return ResponseEntity.badRequest().body("流已经存在！");
        }
    }

    // 移除RTSP流
    @PostMapping("/remove/{streamId}")
    public ResponseEntity<String> removeStream(@PathVariable("streamId") String streamId) {
        boolean success = videoStreamHandler.removeStream(streamId);
        if (success) {
            return ResponseEntity.ok("移除流成功");
        } else {
            return ResponseEntity.badRequest().body("流没有被找到");
        }
    }

    // 启动特定流
    @PostMapping("/start/{streamId}")
    public ResponseEntity<String> startStream(@PathVariable("streamId") String streamId) {
        boolean success = videoStreamHandler.startStream(streamId);
        if (success) {
            return ResponseEntity.ok("视频流已启动");
        } else {
            return ResponseEntity.badRequest().body("启动失败，流可能不存在或已运行");
        }
    }

    // 停止特定流
    @PostMapping("/stop/{streamId}")
    public ResponseEntity<String> stopStream(@PathVariable("streamId") String streamId) {
        boolean success = videoStreamHandler.stopStream(streamId);
        if (success) {
            return ResponseEntity.ok("视频流已停止");
        } else {
            return ResponseEntity.badRequest().body("停止失败，流可能不存在或未运行");
        }
    }

    // 暂停特定流
    @PostMapping("/pause/{streamId}")
    public ResponseEntity<String> pauseStream(@PathVariable("streamId") String streamId) {
        boolean success = videoStreamHandler.pauseStream(streamId);
        if (success) {
            return ResponseEntity.ok("视频流已暂停");
        } else {
            return ResponseEntity.badRequest().body("暂停失败，流可能不存在或未运行");
        }
    }

    // 恢复特定流
    @PostMapping("/resume/{streamId}")
    public ResponseEntity<String> resumeStream(@PathVariable("streamId") String streamId) {
        boolean success = videoStreamHandler.resumeStream(streamId);
        if (success) {
            return ResponseEntity.ok("流已经恢复成功");
        } else {
            return ResponseEntity.badRequest().body("流没有被恢复");
        }
    }

    // 处理Chrome DevTools请求
    // 解决异常：org.springframework.web.servlet.resource.NoResourceFoundException: No static resource .well-known/appspecific/com.chrome.devtools.json.
    @GetMapping("/.well-known/appspecific/com.chrome.devtools.json")
    public ResponseEntity<String> handleChromeDevTools() {
        // 返回空的JSON或者适当的响应
        return ResponseEntity.ok("{}");
    }
}